package com.hz.house.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * <p> 将日期时间转换为天数记录是否连续登录、连续操作等业务
 * @author 10203
 *
 */
public class DaysIntUtil {
	public static final String FORMAT_yyyyMMdd = "yyyyMMdd";
	public static final String FORMAT_HH_mm_ss = "HH:mm:ss";
	public static final String FORMAT_yyMMddHHmmssSSS = "yyMMddHHmmssSSS";
	/**
	 * 	日期转为天数值
	 * @param yyyy_MM_dd
	 * @return
	 */
	public static int getDays(String yyyy_MM_dd) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d=null;
		try {
			d = sdf.parse(yyyy_MM_dd);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		long t = d.getTime();
		return (int) (t/(24*60*60*1000L));
	}

	public static String format(Date date, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static void main(String[] args) {
		System.out.println(format(new Date(), FORMAT_yyyyMMdd));
	}

	/**
	 * 	日期对象转换为天数值
	 * @param time
	 * @return
	 */
	public static int getDays(Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(time);
		return getDays(date);
	}
	
	/**
	 * <p> 给定时间的毫秒值转为天数值
	 * @param mills
	 * @return
	 */
	public static int getDays(long mills) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(mills);
		return getDays(date);
	}

	public static Date dateNextDay(Date date, int next){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, next);
		return c.getTime();
	}

	public static Date setDayTime(Date date, String time){
		if (Objects.isNull(date)){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_HH_mm_ss);
		try {
			Date dt = sdf.parse(time);
			Calendar ct = Calendar.getInstance();
			ct.setTime(dt);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.set(Calendar.HOUR_OF_DAY, ct.get(Calendar.HOUR_OF_DAY));
			c.set(Calendar.MINUTE, ct.get(Calendar.MINUTE));
			c.set(Calendar.SECOND, ct.get(Calendar.SECOND));

			return c.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date getDayStart(Date date){
		if (date == null){
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	public static Date getDayEnd(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}

	public static Date getWeekStart(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		//如果是周末
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
			cal.set(Calendar.DAY_OF_YEAR, -7);
		}else{
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		}
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getWeekEnd(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			cal.add(Calendar.DAY_OF_YEAR, 1);
		}
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

}
